package com.m.job.compents;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.client.job.core.constant.JobReqEnums;
import com.client.job.core.model.MJob;
import com.client.job.core.model.MJobInstance;
import com.client.job.core.model.MJobVo;
import com.client.job.utils.DataResult;
import com.m.job.socket.WebSocketClient;

/**
 * Job客户端处理统一组件(对于到客户端)
 * 
 * @des
 * @author mengxiangzhong
 * @date 2021年9月2日
 *
 */

@Component

public class SocketJobForCLientComponent {

	@Autowired
	private WebSocketClient webSocketClient;

	
	
	
	
	
	
	
	/**
	 * 节点变更
	 * 
	 * @param jobList
	 * @param mJobInstanceList
	 */
	public void updAppCounts(List<MJob> jobList, List<MJobInstance> mJobInstanceList) {
		try {
			Map<String, List<MJob>> jobIdMap = jobList.stream().collect(Collectors.groupingBy(MJob::getJobId));
			// 多少个应用
			Map<String, List<MJobInstance>> severMap = mJobInstanceList.stream()
					.collect(Collectors.groupingBy(MJobInstance::getServerInfo));
			for (List<MJobInstance> list : severMap.values()) {
				List<MJobVo> voList = new ArrayList<MJobVo>();
				Map<String, List<MJobInstance>> jobInstanceIdMap = list.stream()
						.collect(Collectors.groupingBy(MJobInstance::getJobId));
				for (Entry<String, List<MJobInstance>> e : jobInstanceIdMap.entrySet()) {
					if (jobIdMap.get(e.getKey()) != null) {
						MJobVo vo = MJobVo.builder().mJob(jobIdMap.get(e.getKey()).get(0))
								.mJobInstanceList(e.getValue()).build();
						voList.add(vo);
					}
				}
				if (!voList.isEmpty()) {
					DataResult dataResult = DataResult.buildSuccess();
					dataResult.buildRequestType(JobReqEnums.APP_COUNT_UPD.getCode());
					dataResult.setData(voList);
					webSocketClient.sendToUser(JSON.toJSONString(dataResult), list.get(0).getServerInfo());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 节点删除
	 * @param jobList
	 * @param mJobInstanceList
	 * @throws IOException
	 */
	public void delJobInstance(List<MJob> jobList, List<MJobInstance> mJobInstanceList) throws IOException{
		Map<String, List<MJob>> jobIdMap = jobList.stream().collect(Collectors.groupingBy(MJob::getJobId));
		// 多少个应用
		Map<String, List<MJobInstance>> severMap = mJobInstanceList.stream()
				.collect(Collectors.groupingBy(MJobInstance::getServerInfo));
		for (List<MJobInstance> list : severMap.values()) {
			List<MJobVo> voList = new ArrayList<MJobVo>();
			Map<String, List<MJobInstance>> jobInstanceIdMap = list.stream()
					.collect(Collectors.groupingBy(MJobInstance::getJobId));
			for (Entry<String, List<MJobInstance>> e : jobInstanceIdMap.entrySet()) {
				if (jobIdMap.get(e.getKey()) != null) {
					MJobVo vo = MJobVo.builder().mJob(jobIdMap.get(e.getKey()).get(0))
							.mJobInstanceList(e.getValue()).build();
					voList.add(vo);
				}
			}
			if (!voList.isEmpty()) {
				DataResult dataResult = DataResult.buildSuccess();
				dataResult.buildRequestType(JobReqEnums.JOB_REMOVE.getCode());
				dataResult.setData(voList);
				webSocketClient.sendToUser(JSON.toJSONString(dataResult), list.get(0).getServerInfo());
			}
		}			
	}



}
